Skip to content

FedEx provider refactoring#1

Merged
frederik5480 merged 3 commits intomainfrom
ssm/23886-FedEx_REST
May 20, 2025
Merged

FedEx provider refactoring#1
frederik5480 merged 3 commits intomainfrom
ssm/23886-FedEx_REST

Conversation

@StanislavSmetaninSSM
Copy link
Contributor

@StanislavSmetaninSSM StanislavSmetaninSSM commented May 13, 2025

  1. Use REST instead of SOAP

Removed the field "ServiceUrl" - we don't need it anymore, since use the REST API.
Added "Test Mode" checkbox to use sandbox environment for test. Uncheck for production.

  1. Refactoring code
    Renamed some fields according to actual names of parameters in FedEx admin panel.
    Refactored code to use our common Client-Server communication layer which we use in other payment/shipping providers.
    Added logging of each data we send to server and the data which we get from server.
    Some other related changes also were done, but they are mainly related with parameters processing...

Added logo.
Used Net 8.0 Framework.
Activated Nullable as per task description.

The settings of provider now looks like this:
image

The original task: User Story 23886. Rewrite FedEx provider to use REST

1) Use REST instead of SOAP
2) Refactoring code
{
public static string SendRequest(string baseAddress, CommandConfiguration configuration)
{
if (configuration.CommandType is not ApiCommand.CreateAccessToken)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems rather odd - we can never get to SendRequest if it's not CreateAccessToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To SendRequest when we have Access token, we need to use the method with TokenData? tokenData argument.
I can just remove this one SendRequest(string baseAddress, CommandConfiguration configuration) if it looks better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it's a little confusing, if it's only when we have an access token this SendRequest is called, then either we should rename it to something that says so, or remove it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


public static string SendRequest(string baseAddress, CommandConfiguration configuration, TokenData? tokenData)
{
using (var messageHandler = GetMessageHandler())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to avoid all this excess indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

ApiCommand.CreateAccessToken => client.PostAsync(apiCommand, GetFormUrlEncodedContent(configuration)),
ApiCommand.GetRateAndTransitTimes or
ApiCommand.ValidateAddress => client.PostAsync(apiCommand, GetStringContent(configuration)),
_ => throw new NotSupportedException($"Unknown operation was used. The operation code: {configuration.CommandType}.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw NotImplementedException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
catch (HttpRequestException requestException)
{
throw new Exception($"An error occurred during FedEx request. Error code: {requestException.StatusCode}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log the exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged before throwing it.
Exception is catched by try-catch in the caller, so exception should be thrown outside of the method.
Like here:

image

Or here:
image

But I added logging of this error, so we can read it in the logs.

ApiCommand.CreateAccessToken => GetCommandLink("oauth/token"),
ApiCommand.GetRateAndTransitTimes => GetCommandLink("rate/v1/rates/quotes"),
ApiCommand.ValidateAddress => GetCommandLink($"address/v1/addresses/resolve"),
_ => throw new NotSupportedException($"The api command is not supported. Command: {configuration.CommandType}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw NotImplementedException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@frederik5480 frederik5480 merged commit ed6e706 into main May 20, 2025
1 check passed
@frederik5480 frederik5480 deleted the ssm/23886-FedEx_REST branch May 20, 2025 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants